﻿using System;
using System.Data;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CAATS_Registration
{
    public partial class caats_step4 : System.Web.UI.Page
    {
        DataTable dataTable = new DataTable();
        DataRow dataRow;
        DataRow[] filterByIDRows;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenerateDataTable();
                GridView1.DataSource = dataTable;
                GridView1.DataBind();
                Session["dt"] = dataTable;
            }
 
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            Server.Transfer("caats_step3.aspx?name=step3", true); 
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            /// next
            Server.Transfer("caats_step5.aspx?name=step3", true); 

        }

        protected void Button3_Click(object sender, EventArgs e)
        {

        }

        protected void AddMember_Click(object sender, EventArgs e)
        {
            Save();
            dataRow = dataTable.NewRow();
            dataTable.Rows.Add(dataRow);
            Refresh();
 
        }

        protected void GenerateDataTable()
        {
            DataColumn dataColumn;
            dataColumn = new DataColumn("ID", Type.GetType("System.Int32"));
            dataColumn.AutoIncrement = true;
            dataTable.Columns.Add(dataColumn);

            dataColumn = new DataColumn("abc");
            dataTable.Columns.Add(dataColumn);

            dataColumn = new DataColumn("bcd");
            dataTable.Columns.Add(dataColumn);

            dataRow = dataTable.NewRow();
            dataTable.Rows.Add(dataRow);
        }

        protected void Refresh()
        {
            GridView1.DataSource = dataTable;
            GridView1.DataBind();
        }
        protected void Save()
        {
            int id;

            dataTable = Session["dt"] as DataTable;
            foreach (GridViewRow gridViewRow in GridView1.Rows)
            {
                id = int.Parse((gridViewRow.FindControl("lblID") as Label).Text);
                filterByIDRows = dataTable.Select("ID = " + id);
                filterByIDRows[0]["abc"] = (gridViewRow.FindControl("txtAbc") as TextBox).Text;
            }
        }

        protected void gridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id;

            id = Convert.ToInt32(e.CommandArgument);

            switch (e.CommandName)
            {
                case "DeleteData":
                    Delete(id);
                    Refresh();
                    break;
            }
        }

        protected void Delete(int id)
        {
            dataTable = Session["dt"] as DataTable;
            filterByIDRows = dataTable.Select("ID = " + id);

            dataTable.Rows.Remove(filterByIDRows[0]);
        }

    }
}
